package testing.manager;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Properties;

//import testing.module.BoxAndCircle;
import utilities.manager.Runner;
import utilities.module.Module;

public class GenericRunner extends Runner
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1712076549823158366L;
	
	protected ArrayList<Module> myModules = null;

	public GenericRunner(int width, int height, Color backgroundColor, Module... modules) 
	{
		super(width, height, backgroundColor);
		
		if(modules != null && modules.length > 0)
		{
			myModules = new ArrayList<Module>();
			
			for(Module mod : modules)
				myModules.add(mod);
		}
	}

	public void gameUpdate(Properties runnerVars) 
	{
		// What's going on???
		if(myModules != null)
		{
			for(Module mod : myModules)
			{
				if(mod != null) mod.update(null);
			}
		}
	}

	public void drawScreen(Graphics g) 
	{
		// Type cast
		Graphics2D g2 = (Graphics2D) g;
		
		// Get original settings...
		Color c = g2.getColor();
		
		// Draw background
		drawBackgroundColor(g2);
		
		// Draw content
		drawContent(g2);
		
		// Return original settings...
		g2.setColor(c);
	}

	/**
	 * 
	 * @param g2
	 */
	public void drawContent(Graphics2D g2) 
	{
		if(myModules != null)
		{
			for(Module mod : myModules)
			{
				if(mod != null) mod.drawMe(g2);
			}
		}
	}

	/**
	 * 
	 * @param g2
	 */
	public void drawBackgroundColor(Graphics2D g2)
	{
		g2.setColor(this.getBackground());
		g2.fillRect(0, 0, this.getWidth(), this.getHeight());
	}
	
	public Module[] getModules()
	{
		return myModules.toArray(new Module[myModules.size()]);
	}
	
	public Module removeModule(int index)
	{
		return myModules.remove(index);
	}
	
	public void appendModule(Module mod)
	{
		myModules.add(mod);
	}
	
	public void addModule(int index, Module mod)
	{
		myModules.add(index, mod);
	}

	public void keyPressed(KeyEvent e) 
	{
		if(myModules != null)
		{
			for(Module mod : myModules)
			{
				if(mod != null) mod.keyPressed(e);
			}
		}
	}

	public void keyReleased(KeyEvent e) 
	{
		if(myModules != null)
		{
			for(Module mod : myModules)
			{
				if(mod != null) mod.keyReleased(e);
			}
		}
	}
}
